// 上传文件
import {
	baseUrl
} from './config'

export const uploadUrl = `${baseUrl}apim.php?_=DataAccess::upload`;

const upload = async function({
	// #ifdef APP-PLUS || H5
	files,
	// #endif
	// #ifdef H5
	file,
	// #endif
	// #ifdef MP-ALIPAY
	fileType,
	// #endif
	filePath,
	name,
	formData
}) {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			mask: true,
			title: '上传中'
		});
		let data = {
			url: uploadUrl,
			header: {
				'guid': uni.getStorageSync("guid") ? uni.getStorageSync("guid") : ''
			},
			name: 'file',
			complete: function() {
				uni.hideLoading();
			},
			success: uploadFileRes => {
				try {
					var res = uploadFileRes.data;
					if (uni.$u.test.jsonString(res)) {
						resolve(JSON.parse(res))
					}
					if (uni.$u.test.object(res)) {
						resolve(res)
					}
				} catch (e) {
					reject(uploadFileRes.data);
				}
			},
			fail: (e) => {
				reject(e);
			}
		};
		// #ifdef H5
		//有文件对象，一般是H5
		if (file) {
			data.file = file;
		}
		// #endif
		//文件路径
		if (filePath) {
			data.filePath = filePath;
		}
		uni.uploadFile(data);
	})
}

export const goUpload = async (params = {}) => await upload(params)
